﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProfilesView.cs" company="DissingNellemann">
//   
// All rights reserved </copyright>
// <summary>
//   The profiles view.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Website.Models.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Website.CRUDServices;
    using Website.Models.Styling;

    using Location = Website.Models.Domain.Location;
    using Profile = Website.Models.Domain.Profile;
    using ProfileContactType = Website.Models.Domain.ProfileContactType;
    using ProfileRelation = Website.Models.Domain.ProfileRelation;
    using ProfileSubtype = Website.Models.Domain.ProfileSubtype;
    using Region = Website.Models.Domain.Region;

    /// <summary>
    ///     The profiles view.
    /// </summary>
    public class ProfilesView
    {
        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="ProfilesView" /> class.
        /// </summary>
        public ProfilesView()
        {
            this.TableHeader = new TableViewHeader();
            this.Profiles = new List<Profile>();
            this.Relations = new List<ProfileRelation>();
            this.Subtypes = new List<ProfileSubtype>();
            this.Locations = new List<Location>();
            this.Regions = new List<Region>();
            this.ContactTypes = new List<ProfileContactType>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets or sets the contact types.
        /// </summary>
        public List<ProfileContactType> ContactTypes { get; set; }

        /// <summary>
        ///     Gets or sets the locations.
        /// </summary>
        public List<Location> Locations { get; set; }

        /// <summary>
        ///     Gets or sets the profiles.
        /// </summary>
        public List<Profile> Profiles { get; set; }

        /// <summary>
        ///     Gets or sets the regions.
        /// </summary>
        public List<Region> Regions { get; set; }

        /// <summary>
        ///     Gets or sets the relations.
        /// </summary>
        public List<ProfileRelation> Relations { get; set; }

        /// <summary>
        ///     Gets or sets the skip.
        /// </summary>
        public int Skip { get; set; }

        /// <summary>
        ///     Gets or sets a value indicating whether sort ascending.
        /// </summary>
        public bool SortAscending { get; set; }

        /// <summary>
        ///     Gets or sets the subtypes.
        /// </summary>
        public List<ProfileSubtype> Subtypes { get; set; }

        /// <summary>
        ///     Gets or sets the table header.
        /// </summary>
        public TableViewHeader TableHeader { get; set; }

        /// <summary>
        ///     Gets or sets the take.
        /// </summary>
        public int Take { get; set; }

        /// <summary>
        ///     Gets or sets the total profiles.
        /// </summary>
        public int TotalProfiles { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The get profiles list.
        /// </summary>
        /// <param name="OrderBy">
        /// The order by.
        /// </param>
        /// <param name="IDs">
        /// The i ds.
        /// </param>
        /// <param name="view">
        /// The view.
        /// </param>
        /// <returns>
        /// The <see cref="List"/>.
        /// </returns>
        public static List<Profile> GetProfilesList(string OrderBy, List<Guid> IDs, ProfilesView view)
        {
            var Profiles = new List<Profile>();

            CRUDServiceProvider.WithCRUDService(
                client =>
                    {
                        var settings = new SearchSettings();
                        settings.SortAscending = view.SortAscending;
                        settings.OrderBy = OrderBy;
                        var pq = new ProfilesQuery();

                        pq.Settings = settings;

                        if (IDs != null)
                        {
                            if (IDs.Count() == 1)
                            {
                                pq.Settings.IDEquals = IDs[0];
                            }
                            else if (IDs.Count() > 1)
                            {
                                pq.Settings.IDIsIn = IDs.ToArray();
                            }
                        }

                        pq.Settings.Skip = view.Skip;
                        pq.Settings.Take = view.Take;
                        pq.Settings.SortAscending = view.SortAscending;

                        if (!string.IsNullOrWhiteSpace(OrderBy))
                        {
                            pq.Settings.OrderBy = OrderBy;
                        }

                        var pqResult = (ProfilesResult)client.Query(pq);
                        if (pqResult != null)
                        {
                            foreach (CRUDServices.Profile dto in pqResult.List)
                            {
                                if (dto != null)
                                {
                                    Profiles.Add(Profile.Convert(dto, view));
                                }
                            }

                            view.TotalProfiles = pqResult.TotalRowCount;
                        }
                    });
            return Profiles;
        }

        /// <summary>
        ///     Gets alle values related to a profile, though not the profile itself
        /// </summary>
        /// <returns>
        ///     The <see cref="ProfilesView" />.
        /// </returns>
        public static ProfilesView GetRelated()
        {
            var pv = new ProfilesView();

            CRUDServiceProvider.WithCRUDService(
                client =>
                    {
                        var settings = new SearchSettings();
                        settings.SortAscending = true;

                        var prq = new ProfileRelationsQuery();
                        prq.Settings = settings;
                        var prqResult = (ProfileRelationsResult)client.Query(prq);

                        if (prqResult != null)
                        {
                            foreach (CRUDServices.ProfileRelation dto in prqResult.List)
                            {
                                if (dto != null)
                                {
                                    pv.Relations.Add(ProfileRelation.Convert(dto));
                                }
                            }
                        }

                        var ptsq = new ProfileSubtypesQuery();
                        ptsq.Settings = settings;
                        var ptsqResult = (ProfileSubtypesResult)client.Query(ptsq);
                        if (ptsqResult != null)
                        {
                            foreach (CRUDServices.ProfileSubtype dto in ptsqResult.List)
                            {
                                if (dto != null)
                                {
                                    pv.Subtypes.Add(ProfileSubtype.Convert(dto));
                                }
                            }
                        }

                        var lq = new LocationsQuery();
                        lq.Settings = settings;
                        var lqResult = (LocationsResult)client.Query(lq);
                        if (lqResult != null)
                        {
                            foreach (CRUDServices.Location dto in lqResult.List)
                            {
                                if (dto != null)
                                {
                                    pv.Locations.Add(Location.Convert(dto));
                                }
                            }
                        }

                        var rq = new RegionsQuery();
                        rq.Settings = settings;
                        var rqResult = (RegionsResult)client.Query(rq);
                        if (rqResult != null)
                        {
                            foreach (CRUDServices.Region dto in rqResult.List)
                            {
                                if (dto != null)
                                {
                                    pv.Regions.Add(Region.Convert(dto));
                                }
                            }
                        }

                        var pctq = new ProfileContactTypesQuery();
                        pctq.Settings = settings;
                        var pctqResult = (ProfileContactTypesResult)client.Query(pctq);
                        if (pctqResult != null)
                        {
                            foreach (CRUDServices.ProfileContactType dto in pctqResult.List)
                            {
                                if (dto != null)
                                {
                                    pv.ContactTypes.Add(ProfileContactType.Convert(dto));
                                }
                            }
                        }
                    });

            return pv;
        }

        #endregion
    }
}